import React,{Component} from "react";
import style from "./index.module.scss";
import {bookInfo} from "../../common/data/_dataStructure";

import {location_icon,antFilltag_icon, shopcart_icon} from "../../common/icon/svg";
import {getSeller} from "../../serverAPI/index";

class BookShotInfoShop extends Component {
    constructor(props){
        super(props);
        this.state={
            userInfo:{},
            seller:{},
            bookInfo:{},
        }
    }

    static defaultProps = {
        bookInfo,
        ops:[{
            title:"下架",
            icon:"",
            action:null
        }]
    }

    addToCart(){
        sessionStorage.setItem("bookInfo",JSON.stringify(this.props.bookInfo));
        location.href="/shopcart";
    }

    componentDidMount(){
       let temp = localStorage.getItem("loginUserInfo");
       if(temp){
            let userInfo = JSON.parse(temp);
            getSeller(this.props.bookInfo.userID,res=>{
                let {code,msg,data} = res;
                    if(code === "01"){
                        this.setState({userInfo, seller:data});
                    }
                    else alert(msg);
            })
        }
       else this.props.history.push("/login");
    }

    render(){
        let bookInfo = this.props.bookInfo;
       
        return (
            <div className={style["bookshort-root"] + " " + style["loadLeft"]}>
                <div className={style["book-cover-box"]}>
                    <img src={bookInfo.coverSrc}/>
                </div>
                <div className={style["book-info-box"]}>
                    <p className={style["name"]}>{bookInfo.name}</p>
                    <p className={style["freshness"]}>{antFilltag_icon} <span className={style["text"]}>{bookInfo.grade}成新</span></p>
                    <p className={style["location"]}>{location_icon} <span className={style["text"]}>{bookInfo.location}</span></p>
                    <p className={style["price-box"]}>
                        <span>￥{bookInfo.currPrice}</span> <del>￥{bookInfo.originPrice}</del> 
                        {this.state.userInfo.id===this.state.seller.id
                            ? <span className={style["my-sell"]}><span>我的出售</span></span>
                            : <button onClick={()=>{this.addToCart()}}>{shopcart_icon}</button>
                        }
                    </p>
                </div>
            </div>
        )
    }
}

export default BookShotInfoShop;